import streamlit as st
import datetime

st.set_page_config(layout="wide")

download_image = '下载镜像'
manage_container = '管理容器'
make_version = '制作版本'
operation = st.sidebar.radio('操作', (manage_container, make_version, download_image))

class DockerHost:
    def __init__(self, host, name):
        self.host = host
        self.name = name

    def render(self):
        st.markdown(f'#### {self.name}: `{self.host}`')
        # TODO 获取所有容器
        for i in range(3):
            self.render_container(f'name_{i}', f'description_{i}')
        default_description = datetime.datetime.now().strftime("%Y年%m月%d日")
        # TODO 获得所有镜像名
        images = ('1', '2', '3')
        # TODO 镜像必须选择
        image_name = st.selectbox('镜像: ', images, key=f'name_{self.host}')
        self.render_creator('smart-guide-regional', image_name, default_description, 22001)
        self.render_creator('smart-guide-single', image_name, default_description, 22002)

    def render_container(self, name, description):
        column_container, column_start, column_stop, column_delete = st.columns([10, 1, 1, 1])
        # 根据容器运行状态设定按钮状态
        start_button = column_start.button('启动', key=f'start_{self.host}_{name}', disabled=True)
        stop_button = column_stop.button('停止', key=f'stop_{self.host}_{name}')
        delete_button = column_delete.button('删除', key=f'delete_{self.host}_{name}')
        column_container.markdown(f'- 容器{name} {description} [22000](http://{self.host}:22000)')
        if start_button:
            # TODO 启动
            st.write('start')
        if stop_button:
            # TODO 停止
            st.write('stop')
        if delete_button:
            # TODO 删除
            st.write('delete')

    def render_creator(self, default_name: str, image_name: str, default_description: str, default_port: int):
        column_name, column_description, column_port, column_button = st.columns([2, 3, 3, 1])
        # TODO name 不能为空
        name = column_name.text_input('名称: ', default_name, key=f'name_{self.host}_{default_name}')
        description = column_description.text_input('描述: ', default_description, key=f'description_{self.host}_{default_name}')
        port = column_port.number_input('端口', default_port, key=f'{self.host}_{default_name}_port')
        create_button = column_button.button('创建容器', key=f'create_{self.host}_{default_name}')
        if create_button:
            # TODO 创建
            st.write(name, image_name, description, port)


dev_host = DockerHost('172.16.0.142', '开发环境')
test_host = DockerHost('172.16.9.89', '集成环境')

if operation == manage_container:
    st.markdown(f'## {manage_container}')
    st.markdown('> 管理公司内的开发、测试环境中运行的服务实例')
    test_host.render()
    dev_host.render()
    # 1. 选择服务器
    #   1.1 查阅服务器状态
    #   1.2 启动、停止、删除容器
    #   1.3 创建新服务
    #     1.3.1 检查端口是否已经被占用
    #     1.3.2 选择单院模式或区域模式
elif operation == download_image:
    st.title(download_image)
    st.markdown('> 将智能导诊软件部署到医院')
    selected_image = st.selectbox('选择镜像', (1, 2, 3, 4), key='download_image')
    download_image = st.button('下载镜像', key='download_image')
    if download_image:
        st.write('download: ' + selected_image)
    # 1. 列举所有可以下载的版本
    # 2. 输入下载人工号
    # 3. 选择要部署到的目标区域
    # 4. 填写备注
elif operation == make_version:
    st.title(make_version)
    st.markdown('> 为GIT代码仓库中的智能导诊软件代码发布镜像版本')
    select_commit = st.selectbox('选择提交:', (1, 2, 3, 4), key='select_commit')
    tag_input = st.text_input('输入tag: ')
    version_button = st.button('创建', key='version_button')
    if version_button:
        st.write('version: ' + tag_input)
    # 1. 选择git提交
    # 2. 选择数据集
    # 3. 确认容器标签
    # 4. 构建容器并输出日志
    # 5. 运行容器并进行测试
    # 6. 输出测试报告
    # 7. 选择推送容器到远端建立版本或放弃打包
